<?php
class Education extends Ecore_Db {
	var $cache;
	var $clear;
	public function __construct() {		
	}
	public function getAll($intAdtype, $intCategoryId, $intType, $intStatus, $intOption, 
						   $strKey, $strStart, $strEnd, $intOff, $intLimit, 
						   $intProvince=0, $intDistrict=0, &$intTotal) {
		try {				
			$this->prepare('call educations_getall_a(?,?,?,?,?,
													 ?,?,?,?,?,
													 ?,?)');		
			$this->bindParam(1, $intAdtype, PDO::PARAM_INT, 4);
			$this->bindParam(2, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intType, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(5, $intOption, PDO::PARAM_INT, 4);
			$this->bindParam(6, $strKey, PDO::PARAM_STR, 50);
			$this->bindParam(7, $strStart, PDO::PARAM_STR, 10);
			$this->bindParam(8, $strEnd, PDO::PARAM_STR, 10);
			$this->bindParam(9, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(10, $intLimit, PDO::PARAM_INT, 4);
			$this->bindParam(11, $intProvince, PDO::PARAM_INT, 11);
			$this->bindParam(12, $intDistrict, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::getAll - ' .$e->getMessage());			
			
		}
	}
	public function get($intEducateId) {
		try {						
			$this->prepare('call educations_get(?)');		
			$this->bindParam(1, $intEducateId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::get - ' .$e->getMessage());			
			
		}
	}
	public function getAllByCate($intCategoryId, $intProvinceId=0, $isCombo=1) {
		try {						
			$this->prepare('call educations_getall_com(?,?)');		
			$this->bindParam(1, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intProvinceId, PDO::PARAM_INT, 4);
			$arrData 	= $this->fetchAll();
			if($isCombo){
				$arrResult	= array();
				foreach($arrData as $value) {
					$arrResult[$value['educateid']] = $value['name'];
				}
				return $arrResult;
			} else {
				return isset($arrData) ? $arrData : null;
			}
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::getAllByCate - ' .$e->getMessage());			
			
		}
	}	
	public function add($intAdminId, $intUserId, $strName, $intCategoryId, $intSubId,  
						$strLogo, $dblLat, $dblLng, $strAddr, $intDistrict, 
						$intProvince, $strCountry, $strTel, $strFax, $strEmail, 
						$strContent, $strShort, $strWebsite, $intParentId, $strTag, 
						$strFeatures, $intAlbumId, $intType, $strFullAddr='',$arrSpec=null) {
		try {
			$this->prepare('call educations_add(?,?,?,?,?,
												?,?,?,?,?,
												?,?,?,?,?,
												?,?,?,?,?,
												?,?,?,?,?)');
			$this->bindParam(1, $intAdminId		, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intUserId		, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strName		, PDO::PARAM_STR, 255);
			$this->bindParam(4, $intCategoryId	, PDO::PARAM_INT, 11);
			$this->bindParam(5, $intSubId		, PDO::PARAM_INT, 11);
			$this->bindParam(6, $strLogo		, PDO::PARAM_STR, 255);
			$this->bindParam(7, $dblLat			, PDO::PARAM_INT);
			$this->bindParam(8, $dblLng			, PDO::PARAM_INT);
			$this->bindParam(9, $strAddr		, PDO::PARAM_STR, 255);
			$this->bindParam(10, $intDistrict	, PDO::PARAM_INT, 11);
			$this->bindParam(11, $intProvince	, PDO::PARAM_INT, 11);
			$this->bindParam(12, $strCountry	, PDO::PARAM_STR, 2);
			$this->bindParam(13, $strTel		, PDO::PARAM_STR, 50);
			$this->bindParam(14, $strFax		, PDO::PARAM_STR, 50);
			$this->bindParam(15, $strEmail		, PDO::PARAM_STR, 255);
			$this->bindParam(16, $strContent	, PDO::PARAM_STR);
			$this->bindParam(17, $strShort		, PDO::PARAM_STR, 255);			
			$this->bindParam(18, $strWebsite	, PDO::PARAM_STR, 255);			
			$this->bindParam(19, $intParentId	, PDO::PARAM_INT, 11);
			$this->bindParam(20, $strTag		, PDO::PARAM_STR, 255);
			$this->bindParam(21, $strFeatures	, PDO::PARAM_STR);
			$this->bindParam(22, $intAlbumId	, PDO::PARAM_INT, 11);
			$this->bindParam(23, $intType		, PDO::PARAM_INT, 4);			
			$this->bindParam(24, $intType		, PDO::PARAM_INT, 4);			
			$arrResult 		= $this->fetchAll();		
			$intError		= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			$intEducateId 	= isset($arrResult[0]['educateid']) ? $arrResult[0]['educateid'] : 0;
			if($intError == 0) {
				// spec
				$intCoSpec 	= count($arrSpec);
				$strSub		= $intCoSpec > 0 ? Ecore_Function::convertArrToStr($arrSpec) : '';	
				if($intCoSpec > 0) {			
					for($i=0;$i<$intCoSpec;$i++){
						$intSpec = $arrSpec[$i];
						$this->addEducationS($intEducateId, $intCategoryId, $intSpec);
					}
				}	
				// search
				$objSearch	= new Ecore_Search();					
				$strSlug	= Ecore_Rewrite_Education::slug($intEducateId,$strName);
				$objSearch->addUpdate($intEducateId, $intType, $strName, $strFullAddr, $strTag,
									  $strShort, $strImage, $strSlug, $dblLng, $dblLat,
									  Ecore_Function::now(), 1, 3, $intCategoryId, $strSub,
									  $intDistrict, $intProvince, $strCountry, $intUserId);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::add - ' . $e->getMessage());			
		}
	}	
	public function update($intAdminId, $intEducateId, $intUserId, $strName, $intCategoryId,   
							$intSubId, $strLogo, $dblLat, $dblLng, $strAddr,  
							$intDistrict, $intProvince, $strCountry, $strTel, $strFax, 
							$strEmail, $strContent, $strShort, $strWebsite, $intParentId,  
							$strTag, $strFeatures, $intAlbumId, $intType, $strFullAddr='', 
							$arrSpec=null) {
		try {
			$this->prepare('call educations_update(?,?,?,?,?,
													?,?,?,?,?,
													?,?,?,?,?,
													?,?,?,?,?,
													?,?,?,?)');			
			$this->bindParam(1, $intAdminId		, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intEducateId	, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intUserId		, PDO::PARAM_INT, 11);
			$this->bindParam(4, $strName		, PDO::PARAM_STR, VL_NAME_MAXLENGTH);
			$this->bindParam(5, $intCategoryId	, PDO::PARAM_INT, 11);
			$this->bindParam(6, $intSubId		, PDO::PARAM_INT, 11);
			$this->bindParam(7, $strLogo		, PDO::PARAM_STR, VL_NAME_MAXLENGTH);
			$this->bindParam(8, $dblLat			, PDO::PARAM_INT);
			$this->bindParam(9, $dblLng			, PDO::PARAM_INT);
			$this->bindParam(10, $strAddr		, PDO::PARAM_STR, VL_NAME_MAXLENGTH);
			$this->bindParam(11, $intDistrict	, PDO::PARAM_INT, 11);
			$this->bindParam(12, $intProvince	, PDO::PARAM_INT, 11);
			$this->bindParam(13, $strCountry	, PDO::PARAM_STR, 2);
			$this->bindParam(14, $strTel		, PDO::PARAM_STR, 50);
			$this->bindParam(15, $strFax		, PDO::PARAM_STR, 50);
			$this->bindParam(16, $strEmail		, PDO::PARAM_STR, VL_NAME_MAXLENGTH);
			$this->bindParam(17, $strContent	, PDO::PARAM_STR);
			$this->bindParam(18, $strShort		, PDO::PARAM_STR, VL_SHORT_MAXLENGTH);			
			$this->bindParam(19, $strWebsite	, PDO::PARAM_STR, VL_NAME_MAXLENGTH);			
			$this->bindParam(20, $intParentId	, PDO::PARAM_INT, 11);
			$this->bindParam(21, $strTag		, PDO::PARAM_STR, VL_TAG_MAXLENGTH);
			$this->bindParam(22, $strFeatures	, PDO::PARAM_STR);
			$this->bindParam(23, $intAlbumId	, PDO::PARAM_INT, 11);			
			$this->bindParam(24, $intType		, PDO::PARAM_INT, 4);
			$this->bindParam(25, $intType		, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();		
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$strOldImage = isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				// delete image				
				if($strImage != '') {					
					if($strOldImage != '') $this->deleteImg($strOldImage);
				}else {
					$strImage = $strOldImage;
				}
				// spec
				$intCoSpec 	= count($arrSpec);
				$strSub		= $intCoSpec > 0 ? Ecore_Function::convertArrToStr($arrSpec) : '';	
				if($intCoSpec > 0) {
					$this->deleteEducationS($intEducateId);
					for($i=0;$i<$intCoSpec;$i++){
						$intSpec = $arrSpec[$i];
						$this->addEducationS($intEducateId, $intCategoryId, $intSpec);
					}
				}	
				// search
				$objSearch	= new Ecore_Search();					
				$strSlug	= Ecore_Rewrite_Education::slug($intEducateId,$strName);
				$objSearch->addUpdate($intEducateId, $intType, $strName, $strFullAddr, $strTag,
									  $strShort, $strImage, $strSlug, $dblLng, $dblLat,
									  Ecore_Function::now(), 1, 3, $intCategoryId, $strSub,
									  $intDistrict, $intProvince, $strCountry, $intUserId);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::update - ' . $e->getMessage());
			
		}
	}
	public function updateStatus($intAdminId, $intEducateId, $intStatus, $intPerm) {
		try {				
			$this->prepare('call educations_update_status(?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intEducateId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$objSearch = new Ecore_Search();
				$objSearch->updateStatus($intEducateId, 1, $intStatus);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::updateStatus - ' .$e->getMessage());			
			
		}
	}
	public function delete($intAdminId, $intEducateId) {
		try {			
			$this->prepare('call educations_delete(?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intEducateId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$strImage = isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				if($strImage != '') {
					$this->deleteImg($strImage);
				}
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::delete - ' .$e->getMessage());			
			
		}
	}
	public function deleteImg($strImg='') {
		if(file_exists(DIR_EDUCATION_FULL . $strImg))
			@unlink(DIR_EDUCATION_FULL . $strImg);
		if(file_exists(DIR_EDUCATION_THUMB . $strImg))
			@unlink(DIR_EDUCATION_THUMB . $strImg);
	}
/** EDUCATION SPEC */
	public function getAllEducationSpec($intEducateId) {
		try {				
			$this->prepare('call educations_s_getall(?)');
			$this->bindParam(1, $intEducateId, PDO::PARAM_INT, 11);			
			$arrResult 	= $this->fetchAll();
			return $arrResult;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::getAllEducationSpec - ' .$e->getMessage());
		}
	}
	public function addEducationS($intEducateId, $intCategoryId, $intSubId) {
		try {						
			$this->prepare('call educations_s_add(?,?,?)');		
			$this->bindParam(1, $intEducateId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intSubId, PDO::PARAM_INT, 11);
			$this->execute();
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::addEducationS - ' .$e->getMessage());			
			
		}
	}
	public function deleteEducationS($intEducateId) {
		try {						
			$this->prepare('call educations_s_delete(?)');		
			$this->bindParam(1, $intEducateId, PDO::PARAM_INT, 11);
			$this->execute();
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::deleteEducationS - ' .$e->getMessage());			
			
		}
	}
/** COMMENT - RATING */
	public function getCmt($intCmtId) {		
		try {				
			$this->prepare('call educations_cmt_get(?)');					
			$this->bindParam(1, $intCmtId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();			
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::getCmt - ' .$e->getMessage());			
			
		}
	}
	public function getAllCmt($intEducateId, $strKeyword, $strStart, $strEnd, $intOff, $intLimit, &$intTotal) {
		try {				
			$this->prepare('call educations_cmt_getall_a(?,?,?,?,?,?)');					
			$this->bindParam(1, $intEducateId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strKeyword, PDO::PARAM_STR, 50);
			$this->bindParam(3, $strStart, PDO::PARAM_STR, 10);
			$this->bindParam(4, $strEnd, PDO::PARAM_STR, 10);
			$this->bindParam(5, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(6, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::getAllCmt - ' .$e->getMessage());			
			
		}
	}
	public function updateCmt($intAdminId, $intCmtId, $strTitle, $strContent) {
		try {				
			$this->prepare('call educations_cmt_update(?,?,?,?)');					
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intCmtId, PDO::PARAM_STR, 50);
			$this->bindParam(3, $strTitle, PDO::PARAM_STR, 255);
			$this->bindParam(4, $strContent, PDO::PARAM_STR);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : -1;
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::updateCmt - ' .$e->getMessage());			
			
		}
	}
	public function updateStatusCmt($intAdminId, $intEcmtId, $intStatus, $intPerm) {
		try {				
			$this->prepare('call educations_cmt_update_status(?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intEcmtId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intEducateId	= isset($arrResult[0]['educateid']) ? $arrResult[0]['educateid'] : 0;
				
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::updateStatusCmt - ' .$e->getMessage());			
			
		}
	}	
	public function deleteCmt($intAdminId, $intEcmtId) {
		try {				
			$this->prepare('call educations_cmt_delete(?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intEcmtId, PDO::PARAM_INT, 11);			
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intEducateId	= isset($arrResult[0]['educateid']) ? $arrResult[0]['educateid'] : 0;				
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Education::deleteCmt - ' .$e->getMessage());			
			
		}
	}	

	
}